<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useTokenStore } from '@/stores/token'
import {
  Menu as IconMenu,
  House,
  ShoppingCart,
  List,
  User,
  SwitchButton,
  Location
} from '@element-plus/icons-vue'

const route = useRoute()

// 生成面包屑导航数据
const breadcrumbs = computed(() => {
  const paths = route.path.split('/').filter(Boolean)
  const items = [
    {
      title: '首页',
      path: '/admin/dashboard'
    }
  ]
  let fullPath = ''

  paths.forEach((path, index) => {
    fullPath += `/${path}`
    const matchedRoute = route.matched[index]
    if (matchedRoute && matchedRoute.meta.title) {
      items.push({
        title: matchedRoute.meta.title,
        path: fullPath
      })
    }
  })

  return items
})

const router = useRouter()
const tokenStore = useTokenStore()

// 控制侧边栏折叠状态
const isCollapse = ref(false)

// 退出登录
const handleLogout = () => {
  tokenStore.removeToken()
  router.push('/login')
}

// 切换侧边栏折叠状态
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}
</script>

<template>
  <el-container class="layout-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '200px'" class="aside">
      <div class="logo-container">
        <img src="@/assets/logo.png" alt="Logo" class="logo" />
        <span v-show="!isCollapse" class="title">宿舍后台管理</span>
      </div>
      
      <el-menu
        :collapse="isCollapse"
        :collapse-transition="false"
        router
        class="menu"
      >
        <el-menu-item index="/admin/dashboard">
          <el-icon><House /></el-icon>
          <template #title>控制台</template>
        </el-menu-item>
        

        <el-menu-item index="/admin/users">
          <el-icon><User /></el-icon>
          <template #title>用户管理</template>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <el-container>
      <!-- 顶部栏 -->
      <el-header class="header">
        <div class="header-left">
          <el-icon class="fold-btn" @click="toggleSidebar">
            <IconMenu />
          </el-icon>
        </div>
        
        <div class="header-right">
          <el-dropdown trigger="click">
            <span class="user-info">
              <el-icon><User /></el-icon>
              {{ tokenStore.studentInfo.username }}
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handleLogout">
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 主要内容区 -->
      <el-main class="main">
        <router-view />
      </el-main>
      
      <!-- 页脚 -->
      <el-footer height="50px" class="footer">
        <div class="footer-content">
          CreateBy6组@2025
        </div>
      </el-footer>
    </el-container>
  </el-container>
</template>

<style scoped>
.layout-container {
  height: 100vh;
}

.aside {
  background-color: #ffffff;
  transition: width 0.3s;
  overflow: hidden;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

.logo-container {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  background: #f5f7fa;
  border-bottom: 1px solid #e6e6e6;
  transition: all 0.3s;
}

.logo {
  width: 32px;
  height: 32px;
  border-radius: 4px;
}

.title {
  color: #333;
  font-size: 16px;
  font-weight: 600;
  margin-left: 12px;
  white-space: nowrap;
  opacity: 1;
  transition: opacity 0.3s;
}

.menu {
  border-right: none;
  background-color: #ffffff;
  padding: 16px 0;
}

:deep(.el-menu) {
  border-right: none;
}

:deep(.el-menu-item),
:deep(.el-sub-menu__title) {
  height: 50px;
  line-height: 50px;
  color: #606266;
  padding: 0 16px !important;
  margin: 4px 0;

  .el-icon {
    font-size: 18px;
    margin-right: 12px;
    color: #909399;
  }

  &:hover {
    color: #409EFF;
    background-color: #f5f7fa !important;

    .el-icon {
      color: #409EFF;
    }
  }
}

:deep(.el-menu-item.is-active) {
  color: #409EFF;
  background-color: #ecf5ff !important;

  &::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    width: 4px;
    height: 100%;
    background-color: #409EFF;
  }

  .el-icon {
    color: #409EFF;
  }
}

:deep(.el-sub-menu.is-opened) {
  > .el-sub-menu__title {
    color: #fff;
    
    .el-icon {
      color: #409EFF;
    }
  }
}
.header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.fold-btn {
  font-size: 20px;
  cursor: pointer;
  color: #333;
}

.breadcrumb {
  margin-left: 8px;
}

:deep(.el-breadcrumb__item) {
  display: flex;
  align-items: center;
  .el-icon {
    margin-right: 4px;
  }
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #333;
  gap: 8px;
}

.user-info .el-icon {
  margin-right: 4px;
}

.main {
  background-color: #f0f2f5;
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.footer {
  background-color: #fff;
  border-top: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: center;
}


:deep(.el-menu) {
  border-right: none;
}

:deep(.el-menu-item) {
  color: #bfcbd9;
}

:deep(.el-menu-item.is-active) {
  color: #409EFF;
  background-color: #263445;
}

:deep(.el-menu-item:hover) {
  background-color: #263445;
}
</style>